package com.ls.utils;//package com.sf.fengquan.utils;
//
//import com.sf.fengquan.base.BaseResult;
//import com.sf.fengquan.base.BaseResultUtil;
//import com.sf.fengquan.exception.FengQuanException;
//import lombok.Data;
//import org.springframework.stereotype.Component;
//import swiftsdk.SfOssClient;
//import swiftsdk.SwiftConfiguration;
//
//import javax.annotation.PostConstruct;
//import java.io.ByteArrayOutputStream;
//import java.io.File;
//import java.io.FileInputStream;
//import java.io.InputStream;
//import java.util.HashMap;
//import java.util.List;
//import java.util.Map;
//
///**
// * @Classname SfOss
// * @Description 顺丰云OSS
// * @Date 2019/10/11 18:37
// * @Created by pengyu
// */
//@Data
//@Component
//public class SfOss {
//
//    private SwiftConfiguration swiftConfiguration;
//
//    private SfOssClient sfClient;
//
//    public static final String DEFAULT_CONTAINER = "Public";
//
//    @PostConstruct
//    private void init() throws Exception {
////        SwiftConfiguration swiftConfiguration = new SwiftConfiguration ();            //创建配置信息实例
////        swiftConfiguration.setUserName("SFOSS-LEARN-TEST-SIT");                              //设置用户名，一般与账户名（空间名称）相同
////        swiftConfiguration.setAk("dGVzdDp0ZXN0");                            //设置Ak,如果没有相关信息就不填
////        swiftConfiguration.setAccount("SFOSS-LEARN-TEST-SIT");                               //设置账户名，即空间名称
////        swiftConfiguration.setUserKey("8a32955ab951dd2728e8f0d739d260ce");                              //设置密码(SK同密码)
////        swiftConfiguration.setSfossServerUrl("SFOSS-LEARN-TEST-DCN1.osssit.sfdc.com.cn:8080");  //设置服务地址
////        swiftConfiguration.setDefaultContainer(DEFAULT_CONTAINER);
//        swiftConfiguration = new SwiftConfiguration ();
//        swiftConfiguration.setUserName("voucher-ITIM-MS-VOUCHER2");                              //设置用户名，一般与账户名（空间名称）相同
//        swiftConfiguration.setAk("dm91Y2hlci1JVElNLU1TLVZPVUNIRVIyOnZvdWNoZXItSVRJTS1NUy1WT1VDSEVSMg==");                            //设置Ak,如果没有相关信息就不填
//        swiftConfiguration.setAccount("voucher-ITIM-MS-VOUCHER2");                               //设置账户名，即空间名称
//        swiftConfiguration.setUserKey("47032657a93f642d51676f472999e50d");                              //设置密码(SK同密码)
//        swiftConfiguration.setSfossServerUrl("voucher-itim-ms-voucher2-shenzhen-nanshan1.sf-express.com:8080");  //设置服务地址
//        swiftConfiguration.setDefaultContainer(DEFAULT_CONTAINER);
//
//        sfClient = new SfOssClient(swiftConfiguration);          //初始化client
//    }
//
//    /**
//     * 上传并生成URL然后返回给前端
//     */
//    public BaseResult upload(String objectName, String filePath) {
//        try {
//            boolean result = sfClient.uploadFile("pyContainer", objectName, filePath);
//            if (result) {
//                //生成tempurl
//                Map<String, String> containerMeta = new HashMap<String, String>();
//                containerMeta.put("X-Container-Meta-Temp-URL-Key", "key");
//                boolean postResult = sfClient.postContainerMeta(DEFAULT_CONTAINER, containerMeta);
//                if (postResult) {
//                    String url = sfClient.getTempUrl(DEFAULT_CONTAINER, objectName, "GET", 3600);
//                    //需要用户自己拼装域名   System.out.println(swiftConfiguration.getSfossServerUrl() + url);
//                    return BaseResultUtil.success(url);
//                } else {
//                    return BaseResultUtil.error("顺丰云上传失败postContainerMeta：" + filePath);
//                }
//            } else {
//                return BaseResultUtil.error("顺丰云上传失败uploadFile：" + filePath);
//            }
//        } catch (Exception e) {
//            throw new FengQuanException("顺丰云OSS上传失败：" + e.getMessage());
//        }
//    }
//
//  /*  public static void main(String[] args) throws Exception {
//        SwiftConfiguration swiftConfiguration = new SwiftConfiguration();            //创建配置信息实例
//        swiftConfiguration.setUserName("SFOSS-LEARN-TEST-SIT");                              //设置用户名，一般与账户名（空间名称）相同
//        swiftConfiguration.setAccount("SFOSS-LEARN-TEST-SIT");                               //设置账户名，即空间名称
//        swiftConfiguration.setUserKey("8a32955ab951dd2728e8f0d739d260ce");                              //设置密码(SK同密码)
//        swiftConfiguration.setSfossServerUrl("10.203.219.24:8080");  //设置服务地址
////        swiftConfiguration.setAk("");
//        SfOssClient sfClient = new SfOssClient(swiftConfiguration);          //初始化client
//
//        try {
//
////            Map<String, String> accountHeaders = sfClient.headAccountMeta(); //查看元数据
////            for (String key : accountHeaders.keySet()) {
////                System.out.println("KEY:"+ key + " :" + accountHeaders.get(key));
////            }
////            List<String> containers = sfClient.getContainerList(null,100);
////                System.out.println("containers:"+containers);
//
////            boolean t = sfClient.createContainer("pyContainer");
////            System.out.println(t);
//
////            Map<String, String> containerMeta = new HashMap<String, String>();
////            containerMeta.put("X-Container-Meta-Book","english");
////            boolean t = sfClient.postContainerMeta("pyContainer",containerMeta);
////            System.out.println(t);
//
////            Map<String, String> headers = sfClient.headContainerMeta("pyContainer");
////            System.out.println(headers);
//
//
////            boolean t1 = sfClient.uploadFile("pyContainer","aaa.jpg","D:\\aaa.jpg");
////            System.out.println(t1);
//
//            //获取v2token此操作在内网的server上完成
//            String v2Token = sfClient.getV2tokne("pyContainer","testObject");
//
//            System.out.print(v2Token);
//            //设置v2token此操作在外网的app上执行。
//            sfClient.setV2tokne(v2Token);
//
//
//            File file = new File("D:\\pic\\yj.jpg");
//            InputStream out = new FileInputStream(file);
//            boolean t2 = sfClient.uploadObjectV2("pyContainer","aaa.jpg",out);
//            System.out.println(t2);
//
//
////            Map<String,String> objectMeta = sfClient.headObjectMeta("pyContainer","游戏鼠标引擎");
////            System.out.println("objectMeta:"+objectMeta);
//
//
////            try {
////                InputStream in = sfClient.getObject("pyContainer","游戏鼠标引擎");
////                ByteArrayOutputStream baos = new ByteArrayOutputStream();
////                //创建缓存大小
////                byte[] buffer = new byte[10240]; // 1KB
////                //每次读取到内容的长度
////                int len = -1;
////                //开始读取输入流中的内容
////                while ((len = in.read(buffer)) != -1) { //当等于-1说明没有数据可以读取了
////                    baos.write(buffer, 0, len);   //把读取到的内容写到输出流中
////                }
////                //<把字节数组转换为字符串
////                String content = baos.toString();
////                //关闭输入流和输出流
////                in.close();
////                baos.close();
////                //输出字符串结果
////                System.out.println(content);
////            }catch (Exception e){
////                System.out.println(e);
////            }
//
//
////生成tempurl
//
//            Map<String, String> containerMeta = new HashMap<String, String>();
//            containerMeta.put("X-Container-Meta-Temp-URL-Key", "key");
//            boolean t = sfClient.postContainerMeta("pyContainer", containerMeta);
//            if (t) {
//                String url = sfClient.getTempUrl("pyContainer", "aaa.jpg", "GET", 3600);
//                System.out.println(url);
//                //需要用户自己拼装域名
//                System.out.println(swiftConfiguration.getSfossServerUrl() + url);
//            }
//
//
////            Boolean t =sfClient.uploadFileV2(DEFAULT_CONTAINER,"游戏鼠标引擎","E://游戏鼠标引擎.jpg");
////            System.out.print(t);
//        } catch (Exception e) {
//            System.out.print(e);
//        }
//
//    }*/
//
//
//
//    public static void main(String[] args) throws Exception {
//        SwiftConfiguration swiftConfiguration = new SwiftConfiguration();            //创建配置信息实例
//        swiftConfiguration.setUserName("SFOSS-LEARN-TEST-SIT");                              //设置用户名，一般与账户名（空间名称）相同
//        swiftConfiguration.setAccount("SFOSS-LEARN-TEST-SIT");                               //设置账户名，即空间名称
//        swiftConfiguration.setUserKey("8a32955ab951dd2728e8f0d739d260ce");                              //设置密码(SK同密码)
//        swiftConfiguration.setSfossServerUrl("10.203.219.24:8080");  //设置服务地址
////        swiftConfiguration.setAk("");
//        SfOssClient sfClient = new SfOssClient(swiftConfiguration);          //初始化client
//
//        try {
//
//
//            boolean containerResult = sfClient.createContainer("pyContainer");
//            System.out.println("==========containerResult = "+containerResult);
//
//            String objectName = UUIDUtils.generate()+".jpg";
//
//            //获取v2token此操作在内网的server上完成
//            String v2Token = sfClient.getV2tokne("pyContainer",objectName);
//
//            System.out.println("========v2Token = "+v2Token);
//            //设置v2token此操作在外网的app上执行。
//            sfClient.setV2tokne(v2Token);
//
//            File file = new File("D:\\pic\\yj.jpg");
//            InputStream out = new FileInputStream(file);
//            boolean uploadObjectV2Result = sfClient.uploadObjectV2("pyContainer",objectName,out,out.available());
//            System.out.println("===========uploadObjectV2Result = " + uploadObjectV2Result);
//
////生成tempurl
//
//            Map<String, String> containerMeta = new HashMap<String, String>();
//            containerMeta.put("X-Container-Meta-Temp-URL-Key", "key");
//            boolean t = sfClient.postContainerMeta("pyContainer", containerMeta);
//            if (t) {
//                String url = sfClient.getTempUrl("pyContainer", objectName, "GET", 3600);
//                System.out.println("url = " +url);
//                //需要用户自己拼装域名
//                System.out.println(swiftConfiguration.getSfossServerUrl() + url);
//            }
//
//
//        } catch (Exception e) {
//            System.out.print(e);
//        }
//
//    }
//
//}
